<template>
  <div class="user flex align-center no-drag">
    <a-button @click="testApi(1)"
              type='link'>打开调试工具</a-button>
    <a-button @click="testApi(2)"
              type='link'>reload</a-button>

    <div v-if="isLogin"
         class="is-login">
      <img :src="userInfo.profile.avatarUrl">
      <span class="user-name">{{userInfo.profile.nickname}}</span>
      <CaretDownOutlined />
    </div>
    <div v-else
         @click="handleLogin"
         class="is-logout flex align-center">
      <div class="louout-img">
        <UserOutlined />
      </div>
      <span class="user-name">未登录</span>
      <CaretDownOutlined />
    </div>
    <div class="menu">
      <SettingOutlined />
      <SkinOutlined />
      <MailOutlined />
      <a-divider style="background-color:rgba(0,0,0,0.2); margin-left: 12px;"
                 type="vertical" />
    </div>

  </div>
</template>

<script>
import { CaretDownOutlined, SettingOutlined, MailOutlined, SkinOutlined, UserOutlined } from '@ant-design/icons-vue';
import { ref, reactive, getCurrentInstance, computed } from "vue"
import { useStore } from 'vuex'
const { remote, ipcRenderer } = window.require('electron')
export default {
  components: {
    CaretDownOutlined,
    SettingOutlined,
    MailOutlined,
    SkinOutlined,
    UserOutlined
  },
  setup(prop) {
    const { ctx } = getCurrentInstance()
    ipcRenderer.on('login-success', (event, obj) => {
      console.log('ipcRenderer监听到了登录成功', obj);
      // ctx.$store.commit('setUsers', obj)
      store.commit('setUsers', obj)
    });

    const win = remote.getCurrentWindow()
    // const userInfo = computed(() => ctx.$store.state.users || {})
    const store = useStore()
    const userInfo = computed(() => store.state.users || {})
    let isLogin = computed(() => {
      return userInfo.value.profile && userInfo.value.profile.nickname
    })
    console.log(111, isLogin.value, 2222, { ...userInfo.value.profile });
    const testApi = type => {
      switch (type) {
        case 1:
          win.webContents.openDevTools()
          break;

        case 2:
          win.reload()
          break;

        default:
          break;
      }
    }


    const handleLogin = () => {
      // ctx.$router.push('/login');
      ipcRenderer.send('show-login-win')
    }

    return {
      isLogin,
      userInfo,
      handleLogin,
      testApi
    }
  }
}
</script>

<style lang='scss' scoped >
.user {
  color: #fff;
  .is-login,
  .is-logout {
    img {
      width: 32px;
      height: 32px;
      border-radius: 50%;
    }
    .user-name {
      margin: 0 5px;
    }
  }
  .louout-img {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: #e0e0e0;
    font-size: 20px;
    text-align: center;
  }
  .menu {
    .anticon {
      font-size: 18px;
      color: #fff;
      margin: 0 8px;
    }
  }
}
</style>